import * as types from './mutation-types'
import { playMode } from '../assets/js/playMode'
import { shullfle } from '../assets/js/dom'

// 找出歌曲在当前列表下的index
export const indexFind = (list, song) => {
  return list.findIndex(item => item.id === song.id)
}
// 点击歌曲播放
export const selectPlay = ({ commit, state }, { list, index }) => {
  commit(types.SET_SEQUENCE_LIST, list)
  if (state.mode === playMode.random) {
    let randomList = shullfle(list)
    commit(types.SET_PLAY_LIST, randomList)
    index = indexFind(randomList, list[index])
  } else {
    commit(types.SET_PLAY_LIST, list)
  }
  commit(types.SET_PLAYING_STATE, true)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_CURRENT_INDEX, index)
}
// 切换到随机播放模式
export const randomPlay = function ({ commit }, { list }) {
  commit(types.SET_MODE, playMode.random)
  commit(types.SET_PLAYING_STATE, true)
  commit(types.SET_SEQUENCE_LIST, list)
  commit(types.SET_FULL_SCREEN, true)
  let randomList = shullfle(list)
  commit(types.SET_PLAY_LIST, randomList)
  commit(types.SET_CURRENT_INDEX, 0)
}